package com.example.gkcircle.adapter;

import android.app.Activity;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVUser;
import com.example.gkcircle.App;
import com.example.gkcircle.R;
import com.example.gkcircle.bean.Movie;
import com.example.gkcircle.library.object.DetailBook;
import com.example.gkcircle.utils.PicUtil;
import com.example.gkcircle.utils.UniversalImageLoader;

import java.util.List;

/**
 * Created by Jehu on 16/3/29.
 */
public class BookshelfAdapter extends RecyclerView.Adapter<BookshelfAdapter.ViewHolder> implements View.OnClickListener{

    private Activity mActivity;
    private List<DetailBook> mData;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public BookshelfAdapter(Activity activity, List<DetailBook> data){
        this.mActivity=activity;
        this.mData=data;
    }

    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position);
    }
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mActivity.getLayoutInflater().inflate(R.layout.library_shelf_item, null);
        ViewHolder viewHolder=new ViewHolder(v);
        //将创建的View注册点击事件
        v.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final DetailBook detailBook=mData.get(position);
        //将数据保存在itemView的Tag中，以便点击时进行获取
        //找出holderview的实例
        if(holder instanceof ViewHolder) {
            holder.itemView.setTag(position);
            holder.bookname.setText(detailBook.getBookName());
            UniversalImageLoader.getIntance().displayImage(detailBook.getSmallImgUrl(),holder.bookImage,UniversalImageLoader.getPhotorOptions(),UniversalImageLoader.animateFirstDisplayListener);
        }
    }


    @Override
    public int getItemCount() {
        return mData==null?0:mData.size();
    }


    @Override
    public void onClick(View v) {
          //使用getTag方法获取数据
        if(mOnItemClickListener!=null){
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }


    class ViewHolder extends RecyclerView.ViewHolder
    {
        ImageView bookImage;
        TextView bookname;


        public ViewHolder(View itemView){
            super(itemView);
            bookname=(TextView)itemView.findViewById(R.id.library_bookshelf_item_bookname);
            bookImage=(ImageView)itemView.findViewById(R.id.library_bookshelf_item_image);
        }

    }



    //间距
    public static class SpaceItemDecoration extends RecyclerView.ItemDecoration{

        private int top;
        private int bottom;
        private int right;
        private int left;

        public SpaceItemDecoration(int top,int bottom,int left,int right) {
            this.top = top;
            this.bottom=bottom;
            this.right=right;
            this.left=left;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

            outRect.top=top;
            outRect.bottom=bottom;
            outRect.left=left;
            outRect.right=right;
        }
    }

}
